﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using predic.Web.Host.Model;
using predic.Web.Host.Services;
using System.Linq;
using predic.Web.Infrastructure.Commands;

namespace predic.Web.Model
{
    public class StudyDetail : Infrastructure.Model.BaseModel
    {

        private PredicContext ctx;

        #region .ctor
        public StudyDetail(Study selectedStudy)
        {
            ctx = Core.DataContext.Context;

            this.SelectedStudy = selectedStudy;
            GetStudyDetails();

            InitCommands();
        }


        #endregion

        #region properties

        private Study selectedStudy;
        public Study SelectedStudy
        {
            get
            {
                //CurrentDetail
                return selectedStudy;
            }
            set
            {
                selectedStudy = value;
                this.NotifyProperty(a => this.SelectedStudy);
            }
        }

        private Detail currentDetail;
        public Detail CurrentDetail
        {
            get { return currentDetail; }
            set
            {
                currentDetail = value;
                this.NotifyProperty(a => this.CurrentDetail);

                OnDetailChanged();
            }
        }

        #endregion

        #region notification methods

        private void OnDetailChanged()
        {

        }

        #endregion

        #region private methods

        private void GetStudyDetails()
        {
            this.IsBusy = true;
            this.BusyMessage = "GetStudyDetails";

            ctx.Load<Detail>(ctx.GetDetailByIDStudyQuery(selectedStudy.ID),
                (loadOperation) =>
                {
                    this.IsBusy = false;

                    CurrentDetail = loadOperation.Entities.FirstOrDefault<Detail>();

                    if (CurrentDetail == null)
                    {
                        CurrentDetail = new Detail();
                        CurrentDetail.Study = this.SelectedStudy;
                        ctx.Details.Add(CurrentDetail);
                    }
                },
                null);
        }


        #endregion

        #region button commands

        public ButtonCommand SaveCommand { get; private set; }
        public ButtonCommand CloseCommand { get; private set; }

        private void InitCommands()
        {
            SaveCommand = new ButtonCommand(OnSaveCommand);
            CloseCommand = new ButtonCommand(OnCloseCommand);
        }

        private void OnSaveCommand()
        {
            this.IsBusy = true;
            this.BusyMessage = "Save Details";

            ctx.SubmitChanges(
                (submitOperation) =>
                {
                    var err = submitOperation.Error;
                    this.IsBusy = false;
                }
                , null);
        }

        private void OnCloseCommand()
        {
            this.IsBusy = true;
            this.BusyMessage = "Undo";

            ctx.RejectChanges();
            //SelectedStudy = (Study)SelectedStudy.GetOriginal();

            //ctx.Load(
            //    ctx.Studies.w,
            //    (loadOperation) =>
            //    {
            //    }
            //        , null);

            this.IsBusy = false;
        }
        #endregion

    }
}
